package game.groups.menus.mainMenu
{
	import ash.core.Component;
	import ash.core.Entity;
	
	import engine.GameEngine;
	import engine.classes.entity.EntityParser;
	import engine.classes.enumerations.Axis;
	import engine.groups.Group;
	import engine.groups.other.CameraScene;
	import engine.groups.other.CursorGroup;
	import engine.managers.GroupManager;
	import engine.systems.input.interaction.Interaction;
	import engine.systems.motion.draggable.Draggable;
	import engine.systems.motion.draggable.DraggableSystem;
	import engine.systems.motion.motion.Motion;
	import engine.systems.motion.springToTarget.SpringToTargetSystem;
	import engine.systems.motion.wave.Wave;
	import engine.systems.motion.wave.WaveData;
	import engine.systems.render.reflection.ReflectionSystem;
	import engine.systems.render.render.Render;
	import engine.systems.spatial.boundsBind.BoundsBind;
	import engine.systems.spatial.boundsBind.BoundsBindSystem;
	import engine.systems.spatial.edge.Edge;
	import engine.systems.spatial.spatial.Spatial;
	import engine.systems.timeline.timeline.Timeline;
	import engine.systems.timeline.timelineBitmap.TimelineBitmap;
	import engine.systems.timeline.timelineBitmap.TimelineBitmapSystem;
	import engine.systems.timeline.timelineClip.TimelineClip;
	import engine.systems.ui.Percent;
	import engine.systems.ui.slider.Slider;
	import engine.systems.ui.slider.SliderSystem;
	import engine.utils.BitmapUtils;
	import engine.utils.DisplayUtils;
	import engine.utils.GraphicUtils;
	import engine.utils.MathUtils;
	import engine.utils.MotionUtils;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import game.utils.GameUtils;
	
	public class MainMenu extends CameraScene
	{
		private static var loadedOnce:Boolean = false;
		
		public function MainMenu(container:DisplayObjectContainer, parent:Group)
		{
			super(container, parent);
			
			this.load();
		}
		
		override public function destroy():void
		{
			super.destroy();
		}
		
		override public function load():void
		{
			super.load();
		}
		
		override public function loaded():void
		{
			super.loaded();
			
			var wave:Wave;
			var data:WaveData;
			
			this.addSystem(new SpringToTargetSystem());
			this.addSystem(new BoundsBindSystem());
			this.addSystem(new DraggableSystem());
			this.addSystem(new ReflectionSystem());
			this.addSystem(new SliderSystem());
			
			var rect:Sprite = new Sprite();
			rect.graphics.beginFill(0x00FF00);
			rect.graphics.drawCircle(0, 0, 40);
			rect.graphics.endFill();
			rect.x = rect.y = -200;
			this.groupContainer.addChild(rect);
			
			var square:Sprite = new Sprite();
			square.graphics.beginFill(0x660000);
			square.graphics.drawRect(-25, -25, 50, 50);
			square.graphics.endFill();
			square.name = "EntityParserTest";
			this.groupContainer.addChild(square);
			
			this.loadFile("entities.xml", fileLoaded);
		}
		
		private function timeLoaded(clip:MovieClip):void
		{
			this.addSystem(new TimelineBitmapSystem());
			
			//clip.stop();
			clip.mouseEnabled = true;
			clip.mouseChildren = true;
			
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(0x00FF00);
			sprite.graphics.drawCircle(0, 0, 50);
			sprite.graphics.endFill();
			
			var entity:Entity = new Entity("timeTest");
			this.addEntity(entity);
			
			var timeline:Timeline = new Timeline();
			entity.add(timeline);
			timeline.frameRate *= 2;
			timeline.gotoAndPlay("red");
			
			entity.add(new Spatial());
			entity.add(new Render(clip, this.groupContainer));
			entity.add(new TimelineBitmap(0.1));
			entity.add(new Interaction());
			entity.add(new Draggable());
			
			//entity.add(new Button());
			
			//MotionUtils.spatialToTarget(entity, this.getEntity(CursorGroup.CURSOR_ENTITY_NAME));
		}
		
		private function fileLoaded(xml:XML):void
		{
			var handlers:Dictionary = new Dictionary();
			var parser:EntityParser = new EntityParser(this, handlers, true);
			handlers[Render] = parser.parseRender;
			
			parser.parse(xml);
			
			var entity:Entity = this.getEntityByName("EntityParserTest");
			MotionUtils.springToTarget(entity, this.getEntityByName(CursorGroup.CURSOR_ENTITY_NAME), 0.95, 2);
			
			/*
			DisplayUtils.traceContainers(GameEngine.screenManager.stage);
			GameEngine.groupManager.removeScene();
			trace("Entities: ", GameEngine.systemManager.entities.length);
			trace("Systems: ", GameEngine.systemManager.systems.length);
			DisplayUtils.traceContainers(GameEngine.screenManager.stage);
			trace("Groups: ", GameEngine.groupManager.groups.length);
			*/
			/*if(!MainMenu.loadedOnce)
			{
				MainMenu.loadedOnce = true;
				GameEngine.groupManager.setScene(MainMenu);
			}*/
		}
	}
}